package com.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.entity.Notice;
import com.service.NoticeService;

@Controller @RequestMapping("/notice")
public class NoticeController {
	
	@Resource
	NoticeService noticeService;
	
	@RequestMapping("")
	public String link(ModelMap m){		
		List<Notice> noticeList=noticeService.listNotice();
		m.put("noticeList", noticeList);		
		return "Notice/notice-list";	
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String gotoAddPage(ModelMap m) {
		List<Notice> noticeList=noticeService.listNotice();
		m.put("noticeList",noticeList);
		return "Notice/notice-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Notice notice)throws IOException {
		notice.setNoticeStatus(1);
		notice.setNoticeOrder(1);
		notice.setNoticeCreateTime(new Date());
		notice.setNoticeUpdateTime(new Date());
		noticeService.addNotice(notice);
		return "forward:/notice";
	}
	
	@RequestMapping(value="/delete/{id}")
    public String deleteNotice(@PathVariable("id") Integer id)  {
		noticeService.deleteNotice(id);
        return "forward:/notice";
    }
	
	@RequestMapping(value="/edit/{id}")
    public String editNoticeView(@PathVariable("id") Integer id,ModelMap m)  {
  
		Notice notice = noticeService.getNoticeById(id);
        m.put("notice",notice);
        
        //List<Notice> noticeList = noticeService.listNotice(); 
        //m.put("noticeList",noticeList);
        return "Notice/edit";
    }
	
    @RequestMapping(value="/edit",method = RequestMethod.POST)
    public String editNoticeSubmit(Notice notice) throws IOException {
    	notice.setNoticeUpdateTime(new Date());
        noticeService.updateNotice(notice);
        return "forward:/notice";
    }
}
